import 'package:dmz/base/controller/base_controller.dart';
import 'package:dmz/business/find/model/find_model.dart';
import 'package:dmz/request/apis.dart';
import 'package:dmz/request/request_client.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_instance/src/bindings_interface.dart';
import 'package:get/get_rx/get_rx.dart';

class FindPageController extends BaseController {
  @override
  void loadNet() {}

  @override
  void onHidden() {}

  List<String> imageUrls = [];
  RxInt selectPage = 0.obs;
  List<List<FindListData>> allDatas = [[], [], []];
  PageController pageController = PageController();

  @override
  void onInit() {
    getImages();
    getPagedata(0, 0);
    super.onInit();
  }

  Future<void> getImages() async {
    var value = await RequestClient()
        .post<Map>(APIS.promotionalImage, data: {'index': 1, 'size': 20});
    if (value != null) {
      imageUrls =
          (value['list'] as List).map((e) => e['image_url'] as String).toList();
      update();
    }
  }

  Future<void> getPagedata(int tag, int index,
      {bool reload = false, bool changePage = false}) async {
    selectPage.value = tag;
    if (changePage) pageController.jumpToPage(tag);
    if (reload) return update(['find$tag']);
    var value = await RequestClient().post<FindePageData>(APIS.vehicleSkill,
        data: {'index': index, 'size': 20, 'kind': tag});
    if (value != null) {
      index == 0
          ? allDatas[tag] = value.list ?? []
          : allDatas[tag].addAll(value.list ?? []);
      update(['find$tag']);
    }
  }
}

class FindBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut(() => FindPageController());
  }
}
